<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 07.12.11
 * Time: 3:45
 * To change this template use File | Settings | File Templates.
 */

class lessonController extends Core_BaseController
{


    function indexAction($course = -1,$idLesson = -1,$partIndex = -1)
    {
        $permission = array('guest','admin');
               $auth = $this->checkAuth();
               $allow = $this->userHasPermission($permission);
               $this->twig = $this->getRegistry()->twig;

               if ($allow){
                  $role = $this->getAuth()->getUserRoles($auth);
                   $lessonManager = new Lesson_Lesson();

                  $lessonParts = $lessonManager->getLessonParts($idLesson);
                   $max = count($lessonParts);
                   if($partIndex == -1)  $partIndex = 0;
                   if($partIndex > $max-1){
                       $partIndex = $max-1;

                   }
                   $i = 0;
                   foreach($lessonParts as $key=>$value){

                   $submenu[$key]['title'] = $value['Title'];
                   $submenu[$key]['href'] = "lesson/index/$course/$idLesson/$i";
                   $i++;
                   }

                  $data = array('lessonsPart' => $lessonParts[$partIndex],
                                 'course' =>$course,
                                'partIndex' =>$partIndex,
                                'lesson' =>$idLesson,
                                'submenus' =>$submenu,
                                'menuName' => 'Меню курсу',
                  'role' => $role);

                  $data = array_merge($data,$auth);

                 $template = $this->twig->loadTemplate('lesson_index.html.twig');
                   echo $template->render($data);


               }else{
                   $template = $this->twig->loadTemplate('access.html.twig');
                   echo $template->render(array());
               }

    }

    private function validate()
    {
        $err = array();
        if (empty($_POST)) {
            $err[] = 'Введіть будь-даска дані !';
            return $err;
        }

        if (empty($_POST['title'])) {
            $err[] = "Title is empty";
        }
        if (empty($_POST['idlesson'])) {
            $err[] = "idlesson is empty";
        }
        if (empty($_POST['content'])) {
            $err[] = "content is empty";
        }
        if (empty($_POST['lesson'])) {
            $err[] = "lesson is empty";
        }
        return $err;


    }

    function addAction()
    {
         $permission = array('guest','admin');
        $auth = $this->checkAuth();
        $allow = $this->userHasPermission($permission);
        $this->twig = $this->getRegistry()->twig;
        $lessonManager = new Lesson_Lesson();
        if ($allow) {

           $data = array_merge($auth);


             $template = $this->twig->loadTemplate('lesson_add.html.twig');
            echo $template->render($data);
        }   else{
             $template = $this->twig->loadTemplate('access.html.twig');
            echo $template->render(array());
        }

    }
}
